//index.js
//获取应用实例
var app = getApp()
Page({
    data:{
      nowPlay:"当前播放",
      roll:{
        swipera:"",
        swiperb: "",
        swiperc: ""
      },
      introduce:"",
      textMove:20,
      textLeft:"20px",
      nowPlace:"当前景区：伪满皇宫",
      nearby:"附近有6个景点",
      findNum:[],
      now:"A",
      pa:"",
      pb:""
    },
    onLoad:function(){
      var that=this;
      rollImage(that);
      moveWord(that);
      // var thatA = this;
      // statAudio(thatA);
      getPosition(that);
      // console.log("计算"+GetDistance(43.823685, 125.41775, 43.822643, 125.41856))
    },
    playAudio:function(event){
      var that=this;
      var audio=event.currentTarget.dataset.audio;
      var n = event.currentTarget.dataset.index;
      var isplay = event.currentTarget.dataset.isplay;
      var arr=[]; 
      wx.stopBackgroundAudio();
      if(isplay){
        for (var m = 0; m < that.data.findNum.length; m++) {
          if (m != n) {
            
          } else {
            that.data.findNum[m].state = false;
          }
          arr.push(that.data.findNum[m])
        }
      }else{
        for (var m = 0; m < that.data.findNum.length; m++) {
          if (m != n) {
            that.data.findNum[m].state = false;
          } else {
            that.data.findNum[m].state = true;
          }
          arr.push(that.data.findNum[m])
        }
        wx.playBackgroundAudio({
          dataUrl: audio
        })
      }
      this.setData({
        findNum:arr,
        nowPlay: "当前播放:" +arr[n].name
      });
    }
})
function moveWord(that){
  setInterval(function(){
    if(that.data.textMove==-2000){
      that.setData({
        textMove: 20,
        textLeft: that.data.textMove+"px"
      })
    }else{
      that.setData({
        textMove: that.data.textMove - 10,
        textLeft: that.data.textMove - 10 + "px"
      })
    }
  },200)
}
function rollImage(that){
  wx.request({
    url: "https://www.wopelo.la/weTravel/json/image.json",
    header: {
      "content-type": "application/json"
    },
    dataType: "json",
    success: function (res) {
      var arr=[];
      var m = res.data.point.length - 0;
      for (var n = 0; n < m; n++) {
        if(n==0){
          arr.push({ "state": true, "name": res.data.point[n].place, "audio": res.data.point[n].audio, "index": n });
        }else{
          arr.push({ "state": false, "name": res.data.point[n].place, "audio": res.data.point[n].audio, "index": n });
        }
      }
      that.setData({
        roll:{
          swipera: res.data.index.roll.swipera,
          swiperb: res.data.index.roll.swiperb,
          swiperc: res.data.index.roll.swiperc
        },  
        introduce: res.data.introduce,
        findNum:arr
      });
      wx.stopBackgroundAudio();
      wx.playBackgroundAudio({
        dataUrl: arr[0].audio
      })
      that.setData({
        nowPlay: "当前播放:"+arr[0].name
      })
    }
  });
}
function rollImageA(that) {
  wx.request({
    url: "https://www.wopelo.la/weTravel/json/image.json",
    header: {
      "content-type": "application/json"
    },
    dataType: "json",
    success: function (res) {
      var arr = [];
      var m = res.data.point.length - 0;
      for (var n = 0; n < m; n++) {
        if (n == 0) {
          arr.push({ "state": true, "name": res.data.pointa[n].place, "audio": res.data.pointa[n].audio, "index": n });
        } else {
          arr.push({ "state": false, "name": res.data.pointa[n].place, "audio": res.data.pointa[n].audio, "index": n });
        }
      }
      that.setData({
        roll: {
          swipera: res.data.index.roll.swipera,
          swiperb: res.data.index.roll.swiperb,
          swiperc: res.data.index.roll.swiperc
        },
        introduce: res.data.introduce,
        findNum: arr
      });
      wx.stopBackgroundAudio();
      wx.playBackgroundAudio({
        dataUrl: arr[0].audio
      })
      that.setData({
        nowPlay: "当前播放:" + arr[0].name
      })
    }
  });
}
function statAudio(that){
  console.log(that.data.roll)
}
function getPosition(that){
  var x,y,disa,disb;
  setInterval(function(){
    wx.getLocation({
      success: function (res) {
        x = res.latitude;
        y = res.longitude;
        disa = GetDistance(x, y, 43.823685, 125.41775);
        disb = GetDistance(x, y, 43.822643, 125.41856);
        that.setData({
          pa: disa,
          pb: disb
        })
        // console.log("xy " + x+" "+y)
        // console.log("dis "+dis)
        if(disa<disb&&that.data.now!="A"){
          rollImage(that);
          that.setData({
            now:"A"
          })
        } else if (disb<disa&&that.data.now!="B"){
          rollImageA(that);
          that.setData({
            now:"B"
          })
        }
      }
    })
  },1000)
}
function Rad(d){
  return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
}
function GetDistance(lat1, lng1, lat2, lng2) {
  var radLat1 = Rad(lat1);
  var radLat2 = Rad(lat2);
  var a = radLat1 - radLat2;
  var b = Rad(lng1) - Rad(lng2);
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
    Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137;// EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000; //输出为公里
  //s=s.toFixed(4);
  s=s*1000;
  return s;
}